<?php
/**
 * Created by JetBrains PhpStorm.
 * User: vtrinh
 * Date: 10/24/11
 * Time: 3:53 PM
 * To change this template use File | Settings | File Templates.
 */
 ini_set('error_log','/home2/vinhtrin/public_html/error_log');
error_reporting(E_ALL);
require 'DB.php';
require 'facebook.php';

/**
 * @param $key string
 * @param $mixed mixed
 * @return void
 */
function writeToPersistence($key, $mixed){
	$stmt = DB::instance()->prepare("REPLACE INTO `mallsim` (`id`, `blob`) VALUES (:id, :blob)");
	$params['id'] = $key;
	$params['blob'] = serialize($mixed);
	$stmt->execute($params);
}

function getFromPersistence($key){
	$stmt = DB::instance()->prepare("SELECT `blob` FROM `mallsim` WHERE `id` = :id");
	$params['id'] = $key;
	$stmt->execute($params);
	$data = $stmt->fetchAll();
	return unserialize($data[0]['blob']);
}

function isOriginFacebook(){
	$origin = SERVER('HTTP_REFERER');
	if($origin && strstr($origin, 'apps.facebook.com')){
		return true;
	} else {
		return false;
	}
}

function SERVER($key){
	if(isset($_SERVER[$key])){
		return $_SERVER[$key];
	} else {
		return null;
	}
}

function REQUEST($key){
	if(isset($_REQUEST[$key])){
		return $_REQUEST[$key];
	} else {
		return null;
	}
}

function qd(){
	$btString = '';
	$args = func_get_args();

	foreach(debug_backtrace() as $bt){
		$btString .= $bt['file'] . '(' . $bt['line'] . ')' . "\n";
	}
	$fp = fopen(ini_get('error_log'),'a+');
	fwrite($fp,print_r(array($args,$btString),true));
}


function oAuthRedirectForWebsite(){
    $clientCodeUrl = 'https://www.facebook.com/dialog/oauth?scope=email,user_birthday&client_id='.APP_ID.'&redirect_uri=http://vinhtrinh.org/labs/mallsim_server/index.php';
    redirect($clientCodeUrl);
}

function oAuthRedirectForFacebook(){
    $clientCodeUrl = 'https://www.facebook.com/dialog/oauth?scope=email,user_birthday&client_id='.APP_ID.'&redirect_uri=http://apps.facebook.com/proto_personal/labs/mallsim_server/index.php';
    redirect($clientCodeUrl);
}

function redirect($url){
	?>
	<script>
		window.top.location = '<?=$url?>';
	</script>
	<?
	exit();
}

function getMeInfo($accessToken){
    $openGraph_url = 'https://graph.facebook.com';
    $getUrl = $openGraph_url.'/me';

    $params['access_token'] = $accessToken;

    $response = getRequest($getUrl, $params);
    return $response;
}

define('APP_ID','283217508366280');
define('APP_SECRET', '81eb81cc31f65789457c7b86eb0dc0f0');
function getAccessToken($code){
    $client_id = APP_ID;
    $redirect_uri = 'http://vinhtrinh.org/labs/mallsim_server/index.php';
    $client_secret = APP_SECRET;
    $openGraph_url = 'https://graph.facebook.com';
    $accessTokenRequestUrl = $openGraph_url.'/oauth/access_token';

    $params['redirect_uri'] = $redirect_uri;
    $params['client_secret'] = $client_secret;
    $params['client_id'] = $client_id;
    $params['code'] = $code;

    $response = getRequest($accessTokenRequestUrl, $params);
    list($param1, $param2) = explode("&", $response);
    list($key,$field) = explode('=',$param1);

    if($key != 'access_token'){
        throw new Exception("Error trying to get access token".print_r(array($param1,$param2,$key,$field),true));
    }

    return $field;
}

function getRequest($url, $params){
    $getUrl = $url.'?'.http_build_query($params);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $getUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($curl);
    return $response;
}